import Vue from 'vue'
import VueRouter from 'vue-router'

//1.引用文件
import register from '../views/register.vue'
import login from '../views/login.vue'
import myzoe from '../views/mycenter/myzoe.vue'
import Home from '../views/home.vue'
import edit from "../views/mycenter/edit.vue"
import myfocus from '../views/mycenter/myfocus'
import article from '../views/article.vue'

Vue.use(VueRouter)

//2.配置路由
const routes = [
    { path: '/login', component: login },
    { path: '/register', component: register },
    { path: '/myzoe', component: myzoe, meta: { istoken: true } },
    { path: '/', component: Home },
    { path: '/edit', component: edit, meta: { istoken: true } },
    { path: '/myfocus', component: myfocus, meta: { istoken: true } },
    { path: '/article', component: article }
];
//3.实例路由
const router = new VueRouter({
    routes,
    //改变路由模式变成history
    mode: "history",
});

// 守卫导航
router.beforeEach((to, from, next) => {
    // const token = window.localStorage.getItem('token');
    // 如果我进入的地方是个人中心
    //第一种判断是否去往对应的页面判断是否需要令牌验证
    // if (to.path == '/myzoe') {
    //第二种判断在路由配置里面添加数据
    if (to.meta.istoken) {
        // 判断是否有token
        if (localStorage.getItem('token')) {
            // 存在就跳转
            return next();
        } else {
            // 如果不存在则跳转到登陆页面防止重复跳转报错
            return router.push('/login').catch((err) => {});
        }
    }
    //其他的都要执行所有要写next
    return next();
});

//4导出路由
export default router




// 路由步骤
// 1.导入文件
// 2.配置路由
// 3.实例路由
// 4.导出路由